import Vue from 'vue';
import Fly from 'flyio/dist/npm/wx';

import {
	BASE_URL
} from '@/config/index.js';
const fly = new Fly;
const config = {
	timeout: '18000',
	headers: {
		'Content-Type': 'application/json',
	},
	parseJson: true,
	baseURL: BASE_URL,
}

fly.config = config;

fly.interceptors.request.use((request) => {
	const token = uni.getStorageSync('loginToken');
	if (token) {
		request.headers['loginToken'] = token;
	}
})

fly.interceptors.response.use(

	function(response) {
		const data = response.data;
		// 重新登录
		if (data.code === 102) {
			uni.redirectTo({
				url: '/pages/login/login'
			})
		}
		return response
	},
	function(err) {
		uni.showToast({
			title: '网络异常',
			icon: 'none',
			duration: 2000
		});
	}
)

export function GET(url, data = {}) {
	return new Promise((resolve, reject) => {
		fly.get(url, data).then(res => {
			if (res) {
				resolve(res.data);
			}
		}).catch(err => {
			reject(err);
		})
	}).finally(() => {

	})
}

export function POST(url, data = {}) {
	return new Promise((resolve, reject) => {
		fly.post(url, data).then(res => {
			if (res) {
				resolve(res.data);
			}
		}).catch(err => {
			reject(err);
		})
	}).finally(() => {

	})
}